<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form
          :inline="true"
          :model="filterField"
          ref="searchConditions"
          label-width="110px"
        >
          <el-col :lg="8" :xl="6">
            <el-form-item label="违法主体" class="row-padding-bottom">
              <el-input
                v-model="filterField.companyName"
                clearable
                placeholder="请输入违法主体"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item
              label="统一社会信用代码或身份证号"
              class="row-padding-bottom uscc"
            >
              <el-input
                v-model="filterField.uscc"
                clearable
                placeholder="请输入信用代码或身份证"
              ></el-input>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item
                label="法定代表人或负责人"
                class="row-padding-bottom uscc"
              >
                <el-input
                  v-model="filterField.header"
                  clearable
                  placeholder="请输入法人或负责人"
                ></el-input>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px">
          <!-- 查询表格 -->
          <el-button
            class="commonBut saveBtn funComBut noMargin"
            icon="icon iconfont icontianjia"
            @click="addNewEvent"
          >
            <span class="opetareText">添加</span>
          </el-button>
          <el-button
            class="commonBut saveBtn funComBut noMargin"
            icon="icon iconfont icondaoru"
            @click="importExcel"
          >
            <span class="opetareText">导入</span>
          </el-button>
          <el-button
            class="commonBut saveBtn funComBut noMargin"
            icon="icon iconfont icondaochu"
            @click="download"
          >
            <span class="opetareText">导出</span>
          </el-button>
        </div>
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          @sort-change="sortChange"
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="year"
            label="年份"
            sortable="custom"
            :formatter="formatTd"
            min-width="130"
          />
          <el-table-column
            prop="batchNo"
            label="批次号"
            :formatter="formatTd"
            min-width="110"
          />
          <el-table-column
            prop="companyName"
            label="违法主体"
            align="left"
            width="230"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.companyName"
                :columnName="'companyName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="uscc"
            label="统一社会信用代码或身份证号"
            width="220"
            :formatter="formatTd"
          />
          <el-table-column
            prop="address"
            label="地址"
            align="left"
            width="230"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.address"
                :columnName="'address'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="header"
            width="190"
            label="法定代表人或负责人"
            :formatter="formatTd"
          />
          <el-table-column
            prop="eventReason"
            label="主要违法事实"
            min-width="140"
            :formatter="formatTd"
          />
          <el-table-column
            prop="investigate"
            label="查处情况"
            min-width="230"
            align="left"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.investigate"
                :columnName="'investigate'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="left"
            width="190"
            :formatter="formatTd"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-link type="primary" @click="toDetail(scope.$index, scope.row)"
                >查看</el-link
              >
              <el-link type="primary" @click="toEdit(scope.$index, scope.row)"
                >编辑</el-link
              >
              <el-link type="primary" @click="toDel(scope.$index, scope.row)"
                >删除</el-link
              >
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关人员数据"></emptyTable>
          </div>
        </el-table>
        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
      <el-dialog
        title="导入重大劳动保障违法案件"
        :visible="visible"
        :close-on-click-modal="false"
        @close="closeDialog"
        width="600px"
        :modal-append-to-body="false"
        :close-on-press-escape="false"
        class="dialogForm"
        :append-to-body="true"
      >
        <el-form
          :model="info"
          :rules="rules"
          ref="formData"
          label-width="100px"
        >
          <el-row>
            <el-col :span="24">
              <el-form-item label="年份" prop="year">
                <el-date-picker
                  v-model="info.year"
                  type="year"
                  value-format="yyyy"
                  placeholder="请选择"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="批次号" prop="batchNo">
                <el-input
                  v-model="info.batchNo"
                  style="width: 230px"
                  clearable=""
                  placeholder="请输入"
                  class="caseCompTitleInput"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="mainConImport">
          <div class="mainConImportUp">
            选择导入文件
            <el-input
              class="mainConImportUpInput"
              readonly
              v-model="fileName"
            ></el-input>
            <div class="uploadBox">
              <el-button class="uploadImg">选择上传文件</el-button>
              <div class="upLoadDom">
                <form id="ExcelAgreement">
                  <input
                    type="file"
                    id="salaryExcelAgr"
                    name="multipartFile"
                    @change="waterImportfileChange"
                  />
                </form>
              </div>
            </div>
            <!-- 下载《欠薪诉求模板》 -->
            <div class="mainConImportTips">
              请按照模板导入,点击
              <el-link class="downloadTip" @click="downImployee"
                >下载 《重点劳动保障违法事件模板》</el-link
              >
            </div>
          </div>
        </div>
        <div class="caseCompBtn" slot="footer">
          <el-button
            type="primary"
            v-sdc
            :loading="isSaveLoading"
            class="commonBut saveBtn"
            @click="save"
            >保存</el-button
          >
          <el-button type="default" v-sdc class="commonBut" @click="closeDialog"
            >取消</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
import common from '@/kits/common'
export default {
  name: 'bigUnlawerCase',
  mixins: [scrollTable],
  data() {
    return {
      fileName: '',
      visible: false,
      titleText: '重大劳动保障违法案件',
      advanced: false,
      //表格数据
      tableList: [],
      requestUrl: 'api-r/seriousEventHandler/list',
      date: [],
      //搜索条件
      filterField: {
        companyName: '', //企业名称
        uscc: '', //--事件类型（字典）
        header: '', //事件原因（字典）
      },
      pageSize: 20,
      pageNo: 1,

      isShow: false, //列表加载
      loading: true,
      active: -1,
      isShowBtn: false,
      isShowUpdate: '',
      info: {
        batchNo: '',
        year: '',
      },
      rules: {
        batchNo: [
          { message: '批次号不能为空', trigger: 'blur', required: true },
        ],
        year: [{ message: '请选择年份', trigger: 'change', required: true }],
      },
    }
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.isShowBtn =
      this.$store.state.buttonLimitsArr.indexOf('importEvent:add') > -1
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    download() {
      window.location.href =
        common.commonApi +
        'api-r/seriousEventHandler/export?companyName=' +
        this.filterField.companyName +
        '&uscc=' +
        this.filterField.uscc +
        '&header=' +
        this.filterField.header +
        '&sort=' +
        this.filterField.sort +
        '&sortField=' +
        this.filterField.sortField +
        '&access_token=' +
        localStorage.getItem('access_token')
    },
    importExcel() {
      this.$nextTick(() => {
        this.$refs.formData.resetFields()
      })
      this.visible = true
      this.fileName = ''
      this.info = {
        batchNo: '',
        year: '',
      }
      $('#salaryExcelAgr').val('')
    },
    closeDialog() {
      this.visible = false
      this.info = {
        batchNo: '',
        year: '',
      }
      $('#salaryExcelAgr').val('')
    },
    downImployee() {
      this.axios({
        method: 'post',
        url: 'api-r/seriousEventHandler/import/getTemplateUrl',
      })
        .then((response) => {
          if (!response.data.hasError) {
            window.location.href = response.data.result
          }
        })
        .catch((error) => {})
    },
    save() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.isSaveLoading = true
          this.uploadImployee()
        } else {
          return false
        }
      })
    },
    uploadImployee() {
      let that = this
      if (!that.fileName == '') {
        that.active = 1
        that.uploadFileByAjaxSubmit(
          'api-r/seriousEventHandler/import/importExcel',
          that.success,
          that.error
        )
      } else {
        that.$message({
          type: 'warning',
          message: '请先选择上传文件!',
        })
      }
    },
    uploadFileByAjaxSubmit(url, successCallBack, errorCallBack) {
      const that = this
      var token = localStorage.getItem('access_token')
      var options = {
        url: common.uploadUrl + url,
        type: 'POST',
        dataType: 'json',
        headers: { Authorization: 'Bearer ' + token },
        data: that.info,
        success: function (data) {
          $('#salaryExcelAgr').val('')
          successCallBack(data)
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $('#salaryExcelAgr').val('')
          errorCallBack(XMLHttpRequest, textStatus, errorThrown)
        },
      }

      //三种方式均可
      $('#ExcelAgreement').ajaxSubmit(options)
      return false
    },
    success(data) {
      this.isSaveLoading = false
      this.fileName = ''
      this.visible = false
      this.info = {
        batchNo: '',
        year: '',
      }
      this.$nextTick(function () {
        this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      })
      this.$message({
        message: '数据上传成功',
        type: 'success',
      })
    },
    //根据状态码判断是否是401
    error(XMLHttpRequest, textStatus, errorThrown) {
      this.isSaveLoading = false
      if (XMLHttpRequest.status === '401' || XMLHttpRequest.status === '403') {
        localStorage.setItem('access_token', '')
        localStorage.setItem('refresh_token', '')
        localStorage.setItem('userName', '')
        // 只有在当前路由不是登录页面才跳转
        if (router.currentRoute.path !== '/login/loginFirst') {
          router.replace({
            path: '/login/loginFirst',
          })
        }
        //清除tab栏数据
        this.$store.state.sliderBar = []
        localStorage.removeItem('sliderBar')
      } else {
        this.$message({
          message: '数据请求失败',
          type: 'warning',
        })
      }
    },
    // 流水导入 ------start
    waterImportfileChange(e) {
      this.fileName = ''
      var url = window.location.href
      var Filedata = e.target.value
      var files = e.target.files[0]
      if (Filedata == '' || Filedata == undefined) {
        return
      }
      if (!/\.(xls|xlsx)$/.test(Filedata)) {
        this.$message({
          message: 'excel类型必须是.xls,xlsx中的一种',
          type: 'warning',
        })
        return
      }
      this.fileName = files.name
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },

    toDetail(index, row) {
      this.$router.push({
        path: '/home/kMCaseComplaint/bigUnlawerCaseDetail',
        name: 'bigUnlawerCaseDetail',
        query: {
          id: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/kMCaseComplaint/bigUnlawerCase',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    //修改 重大劳动保障违法案件
    toEdit(index, row) {
      this.$router.push({
        path: '/home/kMCaseComplaint/bigUnlawerCaseEdit',
        name: 'bigUnlawerCaseEdit',
        query: {
          id: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/kMCaseComplaint/bigUnlawerCase',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    //新增重大劳动保障违法案件
    addNewEvent() {
      this.$router.push({
        path: '/home/kMCaseComplaint/bigUnlawerCaseEdit',
        name: 'bigUnlawerCaseEdit',
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/kMCaseComplaint/bigUnlawerCase',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    toDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        type: 'warning',
      })
        .then(() => {
          this.del(row.id)
        })
        .catch(() => {
          // this.$message.error("删除失败");
        })
    },
    del(id) {
      this.axios({
        method: 'post',
        url: 'api-r/seriousEventHandler/delete',
        data: { id: id },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.$message({
              message: '删除成功',
              type: 'success',
            })
            this.$nextTick(function () {
              this.$refs.pagination.getTableListData()
            })
          } else {
            this.$message({
              message: response.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 1000,
            message: '删除失败',
            type: 'error',
          })
        })
    },
  },
}
</script>

<style scoped lang="less">
.mainCon {
  height: calc(100% - 67px);
  &Import {
    p {
      font-family: SourceHanSansCN-Medium;
      font-size: 28px;
      color: #333333;
      text-align: center;
      font-weight: bold;
      margin: 60px 0;
    }
    &Up {
      text-align: center;
      //   height: 36px;
      .el-input {
        max-width: 300px;
      }
    }
    &Tips {
      text-align: left;
      height: 14px;
      line-height: 14px;
      font-size: 14px;
      color: #333;
      margin: 24px 0 40px;
      vertical-align: middle;
      /deep/.el-link--inner {
        color: #409eff;
      }
    }
  }
}

.nav ul {
  list-style: none;
}

.nav li {
  display: block;
  float: left;
}
.el-tooltip__popper {
  max-width: 30% !important;
  word-break: break-all;
}
@media only screen and (max-width: 1366px) {
  /deep/.searchInputWidth .el-form--inline .el-form-item__label {
    width: 80px;
  }
}
@import '../../../static/css/filterArea.less';
// 修改专户弹窗样式
.uploadBox {
  display: inline-block;
  position: relative;
  .uploadImg {
    width: 126px;
    height: 36px;
    padding: 0px;
    color: #009588;
    border: 1px solid #009588;
    cursor: pointer;
  }
  .upLoadDom {
    position: absolute;
    width: 126px;
    height: 36px;
    top: 0;
    left: 0;
    opacity: 0;
    line-height: 0px;
    form {
      input {
        width: 96px;
        height: 36px;
        line-height: 0px;
        cursor: pointer;
      }
    }
  }
}
/deep/.el-dialog__footer {
  height: 75px !important;
  padding: 20px !important;
  .commonBut {
    width: 90px;
  }
}

/deep/ .uscc .el-form-item__label {
  line-height: 19px;
}
</style>
